<?php

declare(strict_types=1);

namespace App\Model;

use App\Helpers\AliyunUtil;
use Hyperf\DbConnection\Model\Model;

/**
 * 自动打招呼模型
 */
class UserAutoMsg extends Model
{
    protected $table = 'user_auto_msg';

    //删除
    const STATUS_DEL = 0;
    //正常
    const STATUS_OK = 1;
    //待审核
    const STATUS_ING = -1;
    //审核不通过
    const STATUS_FAIL = 2;

    const TYPE_HOST = [
        'voice' => AliyunUtil::VOICE_HOST,
        'video' => AliyunUtil::VOICE_HOST,
        'image' => AliyunUtil::IMG_HOST,
        'text' => ''
    ];

    protected $fillable = [
        'user_id', 'flag', 'msg_type', 'msg_content', 'duration','voice_tag'
    ];

    /**
     * 添加记录
     */
    static function addRecord($userId = 0, $msgType = 'text', $content = '', $duration = 0,$voiceTag='')
    {
        return self::create([
            'user_id' => $userId,
            'msg_type' => $msgType,
            'msg_content' => $content,
            'duration' => $duration,
            'flag' => self::STATUS_ING,
            'voice_tag' => $voiceTag
        ]);
    }

    /**
     * 选择自动发送的消息
     */
    static function pickUserMsg($userId = 0)
    {
        $data = self::getUserMsgs($userId, true);
        $type = [];
        //图片
        // if (isset($data['image'])) $type = 'image';
        //语音
        // elseif (isset($data['video'])) $type = 'video';
        //语音
        if (isset($data['voice'])) $type[] = 'voice';
        //文字
        if (isset($data['text'])) $type[] = 'text';

        if (!$type) return [
            'msg_type' => 'text',
            'content' => '你好呀',
            'duration' => 0,
            'is_default' => 1,
            'voice_tag' => ''
        ];
        $t = $type[array_rand($type)];
        $item = $data[$t][array_rand($data[$t])];
        return [
            'msg_type' => $t,
            'content' => $item['content'],
            'duration' => $item['duration'],
            'is_default' => 0,
            'voice_tag' => $item['voice_tag']
        ];
    }

    /**
     * 多条打招呼消息
     */
    static function pickMultiUserMsgs($userId = 0)
    {
        $data = self::getUserMsgs($userId, true);
        //①文字+视频  ②文字+视频  ③文字+图片 ④文字+语音
        $types = $msgs = [];
        if (isset($data['text'])) $types[] = 'text';
        if (isset($data['video'])) $types[] = 'video';
        elseif (isset($data['image'])) $types[] = 'image';
        elseif (isset($data['voice'])) $types[] = 'voice';

        $types = array_slice($types, 0, 2);
        foreach ($types as $type) {
            $item = $data[$type][array_rand($data[$type])];
            $msgs[] = [
                'msg_type' => $type,
                'content' => $item['content'],
                'duration' => $item['duration'],
            ];
        }
        return $msgs;
    }

    /**
     * 选取一条某类型的自动消息
     */
    static function pickOneMsg($userId = 0, $msgType = 'text')
    {
        $data = self::getUserMsgs($userId, true, $msgType);
        if (!$data) return [];
        $item = $data[$msgType][array_rand($data[$msgType])];
        $msg = [
            'id' => $item['id'],
            'msg_type' => $msgType,
            'content' => $item['content'],
            'duration' => $item['duration'],
            'voice_tag' => $item['voice_tag']
        ];
        return $msg;
    }

    /**
     * 多条打招呼消息
     */
    static function pickQuickChatMsgs($userId = 0)
    {
        $data = self::getUserMsgs($userId, true);

        //文字+图片+语音
        $types = $msgs = [];
        if (isset($data['text'])) $types[] = 'text';
        if (isset($data['image'])) $types[] = 'image';
        if (isset($data['voice'])) $types[] = 'voice';

        foreach ($types as $type) {
            $item = $data[$type][array_rand($data[$type])];
            $msgs[] = [
                'msg_type' => $type,
                'content' => $item['content'],
                'duration' => $item['duration'],
                'voice_tag' => $item['voice_tag']
            ];
        }

        return $msgs;
    }

    /**
     * 清空文字和图片消息
     */
    static function clearVoiceAndText($userId = 0)
    {
        $where = [
            'user_id' => $userId,
            'flag' => self::STATUS_OK
        ];
        return self::where($where)->whereIn('msg_type', ['voice', 'text'])->update(['flag' => 0]);
    }

    /**
     * 获取用户消息列表
     */
    static function getUserMsgs($userId = 0, $showOk = false, $msgType = '')
    {
        $where = [
            'user_id' => $userId,
            // 'flag' => self::STATUS_OK
        ];
        //消息类型
        $msgType && $where['msg_type'] = $msgType;

        if ($showOk) {
            $where['flag'] = self::STATUS_OK;
            $query = self::where($where);
        } else {
            $query = self::where($where)->whereIn('flag', [self::STATUS_OK, self::STATUS_ING]);
        }

        $data = $query->get()->toArray();
        $ret = [];
        foreach ($data as $item) {
            if (!isset(self::TYPE_HOST[$item['msg_type']])) continue;
            $ret[$item['msg_type']][] = [
                'id' => $item['id'],
                'content' => self::TYPE_HOST[$item['msg_type']] . $item['msg_content'],
                'duration' => $item['duration'],
                'status' => self::getStatus($item['flag']),
                'voice_tag' => $item['voice_tag']
            ];
        }
        return $ret;
    }

    /**
     * 状态
     */
    static function getStatus($flag = 0)
    {
        switch ($flag) {
            case self::STATUS_OK:
                return 0;
            case self::STATUS_ING:
                return 1;
            case self::STATUS_FAIL:
                return 2;
        }
    }

    /**
     * 同类消息的记录
     */
    static function getUserTypeMsgs($userId = 0, $msgType = 'text')
    {
        $where = [
            'user_id' => $userId,
            'msg_type' => $msgType,
            // 'flag' => self::STATUS_OK
        ];
        $data = self::where($where)->whereIn('flag', [self::STATUS_ING, self::STATUS_OK])->orderBy('id', 'desc')->get()->toArray();
        $ret = [];
        foreach ($data as $item) {
            $ret[] = [
                'id' => $item['id'],
                'content' => self::TYPE_HOST[$item['msg_type']] . $item['msg_content'],
                'duration' => $item['duration'],
                'status' => self::getStatus($item['flag']),
                'voice_tag' => $item['voice_tag']
            ];
        }
        return $ret;
    }

    /**
     * 同类消息的记录数量
     */
    static function getMsgTypeNum($userId = 0, $msgType = 'text')
    {
        $where = [
            'user_id' => $userId,
            'msg_type' => $msgType,
            // 'flag' => self::STATUS_OK
        ];
        return self::where($where)->whereIn('flag', [self::STATUS_ING, self::STATUS_OK])->count();
    }

    /**
     * 设置的自动文字消息
     */
    static function getTextMsgs($userId = 0)
    {
        $where = [
            'user_id' => $userId,
            'msg_type' => 'text',
            'flag' => self::STATUS_OK
        ];
        return self::where($where)->pluck('msg_content')->toArray();
    }
}
